import page from "./page.js"
import rightmenu from "./rightmenu.js";

let last_tab_ids=[];

let add_right_menu=function(ele,container_id,tabid){
  rightmenu(ele,[
    {
      "label": "刷新",
      "click": function(){
        tabs.refresh(container_id,tabid);
      }
    },
    {
      "label": "关闭",
      "click": function(){
        tabs.close(container_id,tabid);
      }
    },
    {
      "label": "关闭其他",
      "click": function(){
        tabs.closeothers(container_id,tabid);
      }
    },
    {
      "label": "关闭全部",
      "click": function(){
        tabs.closeall(container_id);
      }
    }
  ]);
}

let tabs={
  open: function(container_id,pageid,label,icon){    
		history.pushState({url:window.location.href.split("?")[0]},'','?pid='+(pageid?pageid:''));
    let tabid="tab_page_"+pageid;
    last_tab_ids.push(tabid);
    let findtab=$("#"+container_id+" .nav-tabs>.tab_"+tabid);
    if(findtab.length>0){
      //再次点击菜单为刷新
      let page_container_id=findtab.data("page_container_id");
      $("#"+container_id+">.nav-tabs-custom>.tab-content>#"+page_container_id).empty();
      findtab.click();
      page.open(page_container_id,pageid);
    }
    else{      
      let page_container_id="page_container_"+new Date().getTime();
      let tab_html='<li class="active tab tab_'+tabid+'" data-page_container_id="'+page_container_id+'" data-pageid="'+pageid+'"><a href="javascript:void(0)"><i class="fa '+icon+'"></i> '+label+' <i class="fa fa-times tab-close" data-tabid="'+tabid+'"></i></a></li>';
      $("#"+container_id+">div>.nav-tabs>li").removeClass("active");
      $("#"+container_id+">.nav-tabs-custom>.nav-tabs").append(tab_html);
      //隐藏其他
      tabs.hideall(container_id);
      //添加新标签
      let content_html='<div class="tab-pane active page_container content_'+tabid+'" id="'+page_container_id+'" style="background: #ecf0f5; padding: 0.75rem;"></div>';
      $("#"+container_id+">.nav-tabs-custom>.tab-content").append(content_html);
      page.open(page_container_id,pageid);

      $("#"+container_id+">div>.nav-tabs>li:last-child").click(function(){
		    history.pushState({url:window.location.href.split("?")[0]},'','?pid='+(pageid?pageid:''));
        last_tab_ids.push(tabid);
        $("#"+container_id+">div>.nav-tabs>li").removeClass("active");
        $(this).addClass("active");
        tabs.show(container_id,page_container_id);
      })

      add_right_menu($("#"+container_id+">div>.nav-tabs>li:last-child"),container_id,tabid);

      $("#"+container_id+">div>.nav-tabs>li:last-child>a>.tab-close").click(function(){
        let tabid=$(this).data("tabid");
        tabs.close(container_id,tabid);
        return false;
      })
    }
  },
  open_by_page_type: function(container_id,tabid,label,icon,type_js,property){
    last_tab_ids.push(tabid);
    let findtab=$("#"+container_id+">.nav-tabs-custom>.nav-tabs>.tab_"+tabid);
    if(findtab.length>0){
      //再次点击菜单为刷新
      let page_container_id=findtab.data("page_container_id");
      $("#"+container_id+">.nav-tabs-custom>.tab-content>#"+page_container_id).empty();
      findtab.click();
      page.open_by_type(page_container_id,type_js,property);
    }
    else{      
      let page_container_id="page_container_"+new Date().getTime();
      let tab_html='<li class="active tab tab_'+tabid+'" data-page_container_id="'+page_container_id+'" data-type_js="'+escape(JSON.stringify(type_js))+'" data-property="'+escape(JSON.stringify(property))+'"><a href="javascript:void(0)"><i class="fa '+icon+'"></i> '+label+' <i class="fa fa-times tab-close" data-tabid="'+tabid+'"></i></a></li>';
      $("#"+container_id+">div>.nav-tabs>li").removeClass("active");
      $("#"+container_id+">.nav-tabs-custom>.nav-tabs").append(tab_html);
      //隐藏其他
      tabs.hideall(container_id);
      //添加新标签
      let content_html='<div class="tab-pane active page_container content_'+tabid+'" id="'+page_container_id+'" style="background: #ecf0f5; padding: 0.75rem;"></div>';
      $("#"+container_id+">.nav-tabs-custom>.tab-content").append(content_html);
      page.open_by_type(page_container_id,type_js,property);

      $("#"+container_id+">div>.nav-tabs>li:last-child").click(function(){
        last_tab_ids.push(tabid);
        $("#"+container_id+">div>.nav-tabs>li").removeClass("active");
        $(this).addClass("active");
        tabs.show(container_id,page_container_id);
      })

      add_right_menu($("#"+container_id+">div>.nav-tabs>li:last-child"),container_id,tabid);

      $("#"+container_id+">div>.nav-tabs>li:last-child>a>.tab-close").click(function(){
        let tabid=$(this).data("tabid");
        tabs.close(container_id,tabid);
        return false;
      })
    }
  },
  show: function(container_id,page_container_id){
    tabs.hideall(container_id);
    //显示这个
    $("#"+container_id+">.nav-tabs-custom>.tab-content>#"+page_container_id).css("height","unset");
    $("#"+container_id+">.nav-tabs-custom>.tab-content>#"+page_container_id).css("overflow","unset");
    $("#"+container_id+">.nav-tabs-custom>.tab-content>#"+page_container_id).css("padding","0.75rem");
  },
  hideall: function(container_id){
    //隐藏所有
    $("#"+container_id+">.nav-tabs-custom>.tab-content>.page_container").css("height","0");
    $("#"+container_id+">.nav-tabs-custom>.tab-content>.page_container").css("overflow","hidden");
    $("#"+container_id+">.nav-tabs-custom>.tab-content>.page_container").css("padding","0");
  },
  refresh: function(container_id,tabid){
    let findtab=$("#"+container_id+">.nav-tabs-custom>.nav-tabs>.tab_"+tabid);
    if(findtab.length>0){
      let pageid=findtab.data("pageid");
      if(pageid){
        this.open(container_id,pageid);
      }
      else{
        let type_js=JSON.parse(unescape(findtab.data("type_js")));
        let property=JSON.parse(unescape(findtab.data("property")));
        this.open_by_page_type(container_id,tabid,null,null,type_js,property);
      }
    }
  },
  close: function(container_id,tabid){
    this.hideall(container_id);
    $("#"+container_id+">.nav-tabs-custom>.nav-tabs>.tab_"+tabid).remove();
    //$("#"+container_id+">.nav-tabs-custom>.tab-content>.content_"+tabid).remove(); //此处注释的目的是amis的bug，remove掉会导致删除提示框不出现
    if($("#"+container_id+">.nav-tabs-custom>.nav-tabs>.active").length==0){ //关闭的是当前打开的
      last_tab_ids.pop();
      let last_tab=$("#"+container_id+">.nav-tabs-custom>.nav-tabs>.tab_"+last_tab_ids.pop());
      if(last_tab.length==0){
        last_tab=$("#"+container_id+">div>.nav-tabs>li:last-child");
      }
      if(last_tab.length==1){
        if(!last_tab.hasClass("active")){
          last_tab.click();
        }
      }
    }
  },
  closecurrent: function(container_id){
    $("#"+container_id+">.nav-tabs-custom>.nav-tabs>.active>a>.tab-close").click();
  },
  closeall: function(container_id){
    last_tab_ids=[];
    $("#"+container_id+">.nav-tabs-custom>.nav-tabs>.tab").remove();
    $("#"+container_id+">.nav-tabs-custom>.tab-content>.tab-pane").remove();
  },
  closeothers: function(container_id,tabid){
    last_tab_ids=[];
    this.hideall(container_id);
    $("#"+container_id+">.nav-tabs-custom>.nav-tabs>.tab:not(.tab_"+tabid+")").remove();
    //$("#"+container_id+">.nav-tabs-custom>.tab-content>.tab-pane:not(.content_"+tabid+")").remove(); //此处注释的目的是amis的bug，remove掉会导致删除提示框不出现
    let tab=$("#"+container_id+">div>.nav-tabs>.tab_"+tabid);
    if(tab.length==1){
      tab.click();
    }
  }
}
export default tabs;